<?php

namespace App\Http\Controllers\Bap;

use App\Http\Controllers\Controller;
use App\Models\Message;
use App\Models\User;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    /**
     * 消息列表
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function message_list(Request $request)
    {
        //用户id
        $uid = $request->get('uid');
        //uid不能为空
        if (empty($uid)) {
            //参数错误
            return respF(1003);
        }
        //查找用户
        $user = User::find($uid);
        //未找到用户
        if (!$user) {
            //用户不存在
            return respF(1001);
        }
        //消息时间大于等于用户注册时间
        $mess = Message::whereIn('uid', [$uid, 0])->where(function ($query) use ($user) {
            $query->where('created_at', '>=', $user->created_at);
        })->orderBy('created_at', 'desc')->paginate(10);
        //没有消息数据
        if (collect($mess)->count() == 0) {
            //未查询到数据
            return respF(1015);
        }
        //处理数据
        $arr = [];
        foreach ($mess as $key => $val) {
            $arr[$key]['mid'] = $val->id;
            $arr[$key]['admin_id'] = $val->admin_id;
            $arr[$key]['uid'] = $val->uid;
            $arr[$key]['is_read'] = $val->is_read;
            $arr[$key]['title'] = $val->title;
            $arr[$key]['content'] = $val->content;
            $arr[$key]['type'] = $val->type;
            $arr[$key]['created_at'] = $val->created_at->format('Y-m-d H:i:s');
        }

        $data['data'] = $arr;
        $data['total'] = $mess->total();
        $data['unread'] = Message::where(['uid' => $uid, 'is_read' => 0])->count();

        if (collect($data)->count() == 0) {
            //未查询到数据
            return respF(1015);
        }

        //点击列表的时候，把对应用户所有消息标为已读
        //Message::where(['uid' => $uid])->update(['is_read' => 1]);

        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }

    /**
     * 消息详情
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function message_info(Request $request)
    {
        //验证必传参数
        if (!$request->has('message_id')) {
            //错误提示
            return respF(1003);
        }
        //消息ID
        $message_id = $request->get('message_id');
        //查找消息
        $mess = Message::find($message_id);
        //消息不存在
        if (!$mess) {
            return respF(1021);
        }

        $mess->update(['is_read' => 1]);

        //消息ID
        $data['mid'] = $mess->id;
        //
        $data['admin_id'] = $mess->admin_id;
        //接收消息用户ID
        $data['uid'] = $mess->uid;
        //标题
        $data['title'] = $mess->title;
        //内容
        $data['content'] = $mess->content;
        //内容
        $data['type'] = $mess->type;
        //发送时间
        $data['created_at'] = $mess->created_at->format('Y-m-d H:i:s');
        //成功提示
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }

}
